package cl.rizepe.playntest.core.listeners;

import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.Pointer.Event;
import pythagoras.f.Point;
import cl.rizepe.playntest.core.effects.EffectFactory;
import cl.rizepe.playntest.core.views.EffectedLayer;

public class DragZoom extends ListenerAdaptor{
	float xStart,yStart;
	float xRelativeStart,yRelativeStart;
	Event startEvent,endEvent, dragEvent;
	 EffectedLayer layerState;
	protected ImageLayer layerShadow;
	Image image;
	
	Point center;
	protected float di,df;
	public DragZoom(EffectedLayer layerState, Image image){
		this.layerState=layerState;
		this.image=image;
		
	}
	
	@Override
	public void onPointerStart(Event event) {
		startEvent=event;
	//	layerShadow=new EffectedImageLayerGL(graphics().createImageLayer(layerState.image()));
		//layerState.parent().add(layerShadow);
	    //layerShadow.setOrigin(layerState.getOrigin().x(),layerState.getOrigin().y());
	    //layerShadow.setTranslate(layerState.getTranslate());
	    //layerShadow.setScale(layerState.getScale());
	    //layerShadow.setAlpha(0.8f);
	    //layerShadow.setVisible(false);
		layerShadow=EffectFactory.createShadow(layerState, image, 0.8f, 2f);

		layerShadow.setVisible(true);
		center = layerState.getOrigin();
		di =center.distanceSq(startEvent.localX(), startEvent.localY());
		if (di<10)
			di=10;
	}

	@Override
	public void onPointerDrag(Event event) {
		dragEvent=event;
		//float df=startEvent.localX()*startEvent.localX()+startEvent.localY()*startEvent.localY();
		//Point p = layerState.getCenter();
		layerShadow.setVisible(true);
		df =center.distanceSq(dragEvent.localX(), dragEvent.localY());
		if (df>0)
			{	
				
				//zoom(df/di*layerState.transform().uniformScale(), layerShadow);
				layerShadow.setScale(df/di*layerState.transform().uniformScale());
			}
	//	System.out.println(locals());
			
	}
	
	protected String locals(){
		String temp="";
		temp+="df:"+df+"   di:"+di;
		temp+="   dsX:"+this.startEvent.localX();
		temp+="   ddX:"+this.dragEvent.localX();
		temp+="   dsY:"+this.startEvent.localY();
		temp+="   ddY:"+this.dragEvent.localY();
		temp+="   cx:"+this.center.x();
		temp+="   cY:"+this.center.y();
		return temp;
	}
	
	@Override
	public void onPointerEnd(Event event){
	//	float df =center.distanceSq(dragEvent.x(), dragEvent.y());
		layerState.setScale(df/di*layerState.transform().uniformScale());
		layerShadow.setVisible(false);

		layerState.updateEffects();
		//layerState.setCenter(center);
	}
	
	

}
